<?php

namespace MyApp\Controller;

use R as R;
use Lib\Annotations\Route as Route;
use Lib\Annotations\Middleware as Middleware;

class AdminController extends \SlimController\SlimController
{
    /**
     * 
     * @Route("/reset", name="reset")
     */
    public function resetAction() {
        $coupons = R::findAll('coupon');
        foreach ($coupons as $subject) {
            R::trash($subject);
        }
//         $this->render('home/adminInsert');
        $this->redirect($this->app->urlFor('adminInsert') . '?secret=haier8');
    }

    /**
     * 
     * @Route("/sharecnt", name="sharecnt")
     */
    public function sharecntAction()
    {
        $app = $this->app;
        $stats = R::findOne('stats', ' sid = 1 ');
        if (!$stats) {
            $stats = R::dispense('stats');
            $stats->sid = 1;
            $stats->nbFriends   = 0;
            $stats->nbTimelines = 0;
        }
        if ($app->request->get('f') == 1) {
            $stats->nbFriends += 1;
        } else {
            $stats->nbTimelines += 1;
        }
        R::store($stats);
        
        header('Content-Type: application/json');
        echo json_encode(['ok'=>1]);
        exit;
    }
    /**
     * 
     * @Route("/adminInsert", name="adminInsert")
     */
    public function insertAction() {
        if ($this->app->request->get('secret') != 'dp917') {
            die('illegal access');
        }
        $coupons = R::findOne('coupon');
        if (empty($coupons)) {
           $con = mysqli_connect('localhost', 'root', '', 'dp917');;
            if (!$con) {
                die('Could not connect: ' . mysql_error());
            }
            
            mysqli_query($con, "CREATE TABLE IF NOT EXISTS coupon (`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, `num` varchar(255), `uid` int(10) unsigned not null default '0', PRIMARY KEY (`id`))");

            $filePath = __DIR__ . '/../../web/data/coupon.txt';
            $couponsText = file_get_contents($filePath);
            $couponsArr = explode("\n", $couponsText);
            $i = 0;
            $couponValues = [];
            foreach ($couponsArr as $coupon) {
                if (empty($coupon)) {
                    continue;
                }
                $coupon = trim($coupon);
                $couponValues[] = "('$coupon')";
                ++$i;
                if ($i === 1000) {
                    mysqli_query($con, 'INSERT IGNORE INTO coupon (num) VALUES ' . implode(',', $couponValues));
                    $couponValues = [];
                    $i = 0;
                }
            }

            mysqli_close($con);
        }
        
        $this->render('admin/adminInsert');
    }
    /**
     * 
     * @Route("/count", name="admin_count")
     */
    public function countAction()
    {
        if ($this->app->request->get('secret') != 'haier'){
            die('illegal access');
        }
        $total = R::count('user');
        $winners = R::findAll('user',' winner = true');
        $this->render('admin/count', array(
            'total' => $total,
            'winners' => $winners
        ));
    }
}